package com.seawaterbt.springbootshiro.controller;

import com.seawaterbt.springbootshiro.entity.User;
import com.seawaterbt.springbootshiro.enums.ErrorCode;
import com.seawaterbt.springbootshiro.result.ResultData;
import com.seawaterbt.springbootshiro.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

@Controller
@Api("登录接口")
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation("首页")
    @RequiresPermissions("login")
    @GetMapping({"","/","index"})
    public String index(){
        return "index";
    }

    @ApiOperation("登录页面")
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    @ResponseBody
    public ResultData login(HttpServletRequest request,String username, String password,boolean rememberMe){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            //request.setAttribute("res", ResultData.failure(ErrorCode.PARAMETER_INCOMPLETE));
            //return "login";
            return ResultData.failure(ErrorCode.PARAMETER_INCOMPLETE);
        }
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            //return "redirect:index";
            return ResultData.failure(ErrorCode.LOGIN_SUCCESS);
        }
        ResultData resultData;
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password,rememberMe);
            subject.login(token);
            User user = (User)subject.getPrincipal();
            //登录次数
            user.setLoginCount(user.getLoginCount()+1);
            //上传登录时间
            user.setLastLoginTime(user.getLoginTime());
            //登录时间
            user.setLoginTime(LocalDateTime.now());
            //修改时间
            user.setUpdateTime(LocalDateTime.now());
            boolean update = userService.updateById(user);
            resultData = update?ResultData.ok(ErrorCode.LOGIN_SUCCESS):ResultData.failure(ErrorCode.SERVER_SYSTEM_ERROR);
        } catch (IncorrectCredentialsException e) {
            resultData = ResultData.failure(ErrorCode.PASSWORD_ERROR.getCode(),"密码错误");
        } catch (LockedAccountException e) {
            resultData = ResultData.failure(ErrorCode.USER_BE_FROZEN.getCode(),"该用户已被冻结");
        } catch (AuthenticationException e) {
            resultData = ResultData.failure(ErrorCode.USER_NOT_EXIST.getCode(),"该用户不存在");
        } catch (Exception e) {
            e.printStackTrace();
            resultData = ResultData.failure();
        }
        return resultData;
        //System.out.println(resultData.getMsg());
        //request.setAttribute("res",resultData);
        /*if(resultData.getCode() == ErrorCode.LOGIN_SUCCESS.getCode()) {
            return "index";
        }*/
        //return "redirect:login";
    }

    //@RequestMapping(value = "/authc",method = {RequestMethod.POST,RequestMethod.GET})
    @GetMapping("/authc")
    public @ResponseBody String authc(){
        return "请先登录";
    }

}
